<?php

namespace app\api\controller;

use app\services\ReservationService;
use plugin\admin\app\model\Reservation;
use support\Db;
use support\Request;

class ReservationController
{
    public function add(Request $request)
    {
        Db::beginTransaction();
        try {
            $data = $request->post();

            $validate = new \app\api\validation\Reservation();

            if (!$validate->check($data)) {
                throw new \Exception($validate->getError());
            }

            if (!is_array($data['thumbs']) || count($data['thumbs']) == 0) {
                throw new \Exception('商品图片不能为空');
            }

            ReservationService::add($data);

            Db::commit();
            return ok_rsp('成功');
        } catch (\Exception $e) {
            Db::rollBack();
            return err_rsp($e->getMessage());
        }
    }
}